import { makeAutoObservable, runInAction } from 'mobx';
import type { TreeDataNode } from 'antd';
import certificateApi from '../../api/certificateApi';

export default class Certificate {

  loading: boolean;
  detailLoading: boolean;
  saving: boolean;
  testing: boolean;
  userTesting: boolean;
  serverIps: string;
  data: any[];
  record: any;
  certificateRecord: any;
  testData: TreeDataNode[];
  users: any[];

  constructor() {
    this.loading = false;
    this.detailLoading = false;
    this.saving = false;
    this.testing = false;
    this.userTesting = false;
    this.serverIps = '';
    this.data = [];
    this.data = [];
    this.record = null;
    this.certificateRecord = null;
    this.testData = [];
    this.users = [];
    makeAutoObservable(this);
  }

  async fetchData() {
    if (this.loading) {
      return;
    }

    runInAction(() => {
      this.loading = true;
    });
    const [res, ipRes] = await Promise.all([
      certificateApi.query(),
      certificateApi.serverIps()
    ]);

    const list: any[] = (res as any).data || [];
    list.forEach((item: any) => {
      item.id = (item.certificate || {}).id || '';
      item.companyName = (item.company || {}).name || '';
      item.count = (item.details || []).length;
    });
    this.selectRecord(null);
    const serverIps = (ipRes as any).data || '[unknown]';
    runInAction(() => {
      this.data = list;
      this.loading = false;
      this.serverIps = serverIps;
    });
  }

  async fetchDetail() {
    if (this.detailLoading) {
      return;
    }

    runInAction(() => {
      this.detailLoading = true;
    });
    const res: any = await certificateApi.find(this.record?.id);
    const record: any = res.data;
    record.id = (record.certificate || {}).id || '';
    record.companyName = (record.company || {}).name || '';
    record.count = (record.details || []).length;
    runInAction(() => {
      this.record = record;
      this.detailLoading = false;
    });
  }

  addData(companyId: string) {
    this.setSaving(true);
    return certificateApi.add(companyId);
  }

  saveData(id: string, data: any[]) {
    this.setSaving(true);
    return certificateApi.save(id, data);
  }

  deleteData() {
    return certificateApi.delete(this.record ? this.record.id : '<empty>');
  }

  async test(data: any) {
    if (this.testing) {
      return;
    }

    runInAction(() => {
      this.testing = true;
      this.testData = [];
      this.users = [];
    });
    const res: any = await certificateApi.test(data);
    runInAction(() => {
      this.testData = res.data || [];
      this.testing = false;
    });
  }

  async testUsers(departmentId: string, data: any) {
    if (this.userTesting) {
      return;
    }

    runInAction(() => {
      this.userTesting = true;
      this.users = [];
    });
    const res: any = await certificateApi.testUsers(departmentId, data);
    runInAction(() => {
      this.users = res.data || [];
      this.userTesting = false;
    });
  }

  deleteDetail() {
    if (this.certificateRecord) {
      let index: number = -1;
      this.record.details.forEach((item: any, i: number) => {
        if (item.id === this.certificateRecord.id) {
          index = i;
        }
      });
      if (index >= 0) {
        const record = { ...this.record}
        record.details.splice(index, 1);
        this.selectRecord(record);
        this.selectCertificateRecord(null)
      }
    }
  }

  selectRecord(record: any) {
    runInAction(() => {
      this.record = record
    });
  }

  selectCertificateRecord(record: any) {
    if (record) {
      const data: any = record.data ? JSON.parse(record.data) : {};
      record = { ...record, ...data };
    }
    runInAction(() => {
      this.certificateRecord = record;
    });
  }

  setSaving(saving: boolean) {
    runInAction(() => {
      this.saving = saving;
    });
  }

};
